/*
 *  Copyright (C) 2012 Компания ВИТ. web: http://diasled.ru Е-mail:  vit@diasled.ru
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * FSensorZones.java
 *
 * Created on Mar 12, 2012, 11:06:57 AM
 */
package ru.apertum.diasled.forms;

import com.jogamp.newt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import org.jdesktop.application.Action;
import ru.apertum.diasled.redactor.CZone;
import ru.apertum.diasled.redactor.SensorDraw;
import ru.apertum.diasled.redactor.SensorZoneTableModel;

/**
 *
 * @author Evgeniy Egorov
 */
public class FEditorSensorZone extends javax.swing.JDialog {

    final static private JComboBox<Integer> cbi = new JComboBox<>();
    final static private TableCellEditor editori = new DefaultCellEditor(cbi);
    final static private JComboBox<Double> cbd = new JComboBox<>();
    final static private TableCellEditor editord = new DefaultCellEditor(cbd);

    static {
        cbi.setModel(new DefaultComboBoxModel<>(new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}));
        cbd.setModel(new DefaultComboBoxModel<>(new Double[]{new Double(1), new Double(0.9), new Double(0.8), new Double(0.75), new Double(0.7), new Double(0.5), new Double(0.3), new Double(0.25), new Double(0.2), new Double(0.1)}));
    }
    private boolean result = false;

    public boolean isOK() {
        return result;
    }

    public static FEditorSensorZone getInstance() {
        FSensorZonesHolder.INSTANCE.result = false;
        return FSensorZonesHolder.INSTANCE;
    }

    private static class FSensorZonesHolder {

        private static final FEditorSensorZone INSTANCE = new FEditorSensorZone(null, true);
    }

    /** Creates new form FSensorZones */
    private FEditorSensorZone(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        try {
            setIconImage(ImageIO.read(DiasledView.class.getResource("/ru/apertum/diasled/forms/resources/redactor.png")));
        } catch (IOException ex) {
            System.err.println(ex);
        }

        // свернем по esc
        getRootPane().registerKeyboardAction(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                result = false;
                setVisible(false);
            }
        },
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                JComponent.WHEN_IN_FOCUSED_WINDOW);

        table.setModel(new SensorZoneTableModel(null));

        final TableColumnModel cm = table.getColumnModel();
        TableColumn tc = cm.getColumn(0);
        tc.setCellEditor(editori);
        tc = cm.getColumn(1);
        tc.setCellEditor(editord);
    }

    public void loadConfig(final SensorDraw sensor) {
        setTitle("Зоны и коэффициенты сенсора. Логический номер " + sensor.getLogicalNum());
        ((SensorZoneTableModel) (table.getModel())).setZone(sensor.getZone());
        ((SensorZoneTableModel) (table.getModel())).fireTableDataChanged();
    }

    public void saveConfig(SensorDraw sensor) {
        sensor.setZone(((SensorZoneTableModel) (table.getModel())).getZone());
        ((SensorZoneTableModel) (table.getModel())).setZone(sensor.getZone());
        ((SensorZoneTableModel) (table.getModel())).fireTableDataChanged();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        buttonOK = new javax.swing.JButton();
        buttonCancel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();

        jPopupMenu1.setName("jPopupMenu1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(ru.apertum.diasled.Diasled.class).getContext().getActionMap(FEditorSensorZone.class, this);
        jMenuItem1.setAction(actionMap.get("addZone")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jPopupMenu1.add(jMenuItem1);

        jMenuItem2.setAction(actionMap.get("removeZone")); // NOI18N
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        jPopupMenu1.add(jMenuItem2);

        setName("Form"); // NOI18N

        jPanel1.setBorder(new javax.swing.border.MatteBorder(null));
        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ru.apertum.diasled.Diasled.class).getContext().getResourceMap(FEditorSensorZone.class);
        buttonOK.setText(resourceMap.getString("buttonOK.text")); // NOI18N
        buttonOK.setName("buttonOK"); // NOI18N
        buttonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOKActionPerformed(evt);
            }
        });

        buttonCancel.setText(resourceMap.getString("buttonCancel.text")); // NOI18N
        buttonCancel.setName("buttonCancel"); // NOI18N
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(209, Short.MAX_VALUE)
                .addComponent(buttonCancel)
                .addGap(18, 18, 18)
                .addComponent(buttonOK)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonOK)
                    .addComponent(buttonCancel))
                .addContainerGap())
        );

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Title 1", "Title 2"
            }
        ));
        table.setComponentPopupMenu(jPopupMenu1);
        table.setName("table"); // NOI18N
        table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        table.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(table);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                .addGap(0, 0, 0)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        result = false;
        setVisible(false);
    }//GEN-LAST:event_buttonCancelActionPerformed

    private void buttonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOKActionPerformed
        result = true;
        setVisible(false);
    }

    @Action
    public void addZone() {
        ((SensorZoneTableModel) (table.getModel())).getZone().add(new CZone());
        ((SensorZoneTableModel) (table.getModel())).fireTableDataChanged();
    }//GEN-LAST:event_buttonOKActionPerformed

    @Action
    public void removeZone() {
        if (table.getSelectionModel().getLeadSelectionIndex() != -1) {
            ((SensorZoneTableModel) (table.getModel())).getZone().remove(table.getSelectionModel().getLeadSelectionIndex());
            ((SensorZoneTableModel) (table.getModel())).fireTableDataChanged();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonOK;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables
}
